package com.appstore.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Filecopy is used to copy file.
 * @author zhbhun
 *
 */
public class FileCopy {

	public static final int BUFFER_SIZE = 16 * 1024;

	/**
	 * copy src to dst.
	 * @param src 
	 * @param dst
	 * @throws Exception
	 */
	public static void copy(File src, File dst) throws Exception {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src),
				        BUFFER_SIZE);
				out = new BufferedOutputStream(new FileOutputStream(dst),
				        BUFFER_SIZE);
				byte[] buffer = new byte[BUFFER_SIZE];
				while (in.read(buffer) > 0) {
					out.write(buffer);
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			throw new Exception("File copy error.", e);
		}
	}
}
